package com.zhangmeng.tools.controller;

import com.zhangmeng.tools.utils.AlertUtils;
import com.zhangmeng.tools.utils.CronUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;

import java.util.List;


/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-03-06 14:44
 */
@Slf4j
public class CronController {


    @FXML
    private TextField cron;

    @FXML
    private TextArea result_show;

    @FXML
    private Button execute;

    @FXML
    private Button verify_cron;

    @FXML
    private TextField execute_num;

    @FXML
    public void initialize() {
        verify_cron.setOnAction(event -> {
            if (cron.getText().length() == 0){
                AlertUtils.alert_warning("请输入cron表达式再试!");
                return;
            }
            verify_cron(cron.getText());
        });

        execute.setOnAction(event -> {
            if (cron.getText().length() == 0){
                AlertUtils.alert_warning("请输入cron表达式再试!");
                return;
            }
            if (execute_num.getText().length() == 0){
                AlertUtils.alert_warning("请输入cron表达式执行次数!");
                return;
            }
            List<String> lists = CronUtil.getExecutionTimeByNum(cron.getText(), Integer.parseInt(execute_num.getText()));
            result_show.setText(null);
            for (String line : lists) {
                add_message(line);
            }
        });
    }

    public void verify_cron(String cron){
      boolean flag =  CronExpression.isValidExpression(cron);
      if (flag){
          AlertUtils.alert_msg("cron表达式正确!");
      }else {
          AlertUtils.alert_warning("cron表达式错误!");
      }
    }

    public void add_message(String line){
        result_show.appendText("cron表达式执行结果:-----------------------------------------" + line);
        result_show.appendText(System.lineSeparator());
    }
}
